//
//  AD_PresentationInfo.m
//  ActiveDeck
//
//  Created by Laura McBride on 9/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AD_PresentationInfo.h"
#import "GDataXMLNode.h"
#import "HttpRequest.h"

@implementation AD_PresentationInfo
@synthesize iconAddress,pName,presentationID,pStatus;


#define Locked 0
#define History 1
#define Anonymous 2

- (NSString*)FetchPInfo:(NSString *)PID
{
    NSError *loadError;
    
    NSString *URLInfo = [NSString stringWithFormat:@"%@/%@/%@", [HttpRequest GetSvcURL], @"Config.svc/GetPresentationInfo", PID];
    
    
    NSString *URLContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:URLInfo] encoding:NSUTF8StringEncoding error:&loadError];
    
    return URLContent;
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [[AD_PresentationInfo alloc] init];
    if( self != nil )
    {
        //decode properties, other class vars
        self.presentationID = [[decoder decodeObjectForKey:@"presentationID"]intValue];
        self.pStatus = [[decoder decodeObjectForKey:@"pStatus"]intValue];
        self.iconAddress = [decoder decodeObjectForKey:@"iconAddress"];
        self.pName = [decoder decodeObjectForKey:@"pName"];
    }
    return self;
}



- (void) encodeWithCoder: (NSCoder *)coder
{
    [coder encodeObject:iconAddress forKey:@"iconAddress"];
    [coder encodeObject:pName forKey:@"pName"];
    [coder encodeObject:[NSNumber numberWithInt:presentationID] forKey:@"presentationID"];
    [coder encodeObject:[NSNumber numberWithInt:pStatus] forKey:@"pStatus"]; 
}

-(id)GetPresentADInfo:(NSString *)PID
{
    NSString *PresentationID = [[NSString alloc] initWithString:PID];
    
    NSString *URLContent = [self FetchPInfo:PresentationID];
    
    
    if(URLContent == NULL )
    {  
        UIAlertView *alertMSG = [UIAlertView alloc];
        alertMSG = [alertMSG initWithTitle:@"Internet Connection Failure"
								   message:@"Please check your Internet connection and try again."
								  delegate:self 
						 cancelButtonTitle:@"OK" 
						 otherButtonTitles:nil];
		
		[alertMSG show];
        [alertMSG release];
        
		return NULL;
    }
    
    NSString *pInfo = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>%@",URLContent];
    
    
    NSString *searchFor = @"Invalid Presentation ID";
	NSRange range;
	range = [pInfo rangeOfString:searchFor];
	
	if (range.location != NSNotFound)
        return NULL;

    
    iconAddress = [[NSString alloc] initWithFormat:@"%@/Config.svc/Logo/%@", [HttpRequest GetSvcURL], PresentationID];
    
    NSDictionary *namespaces = [NSDictionary dictionaryWithObjectsAndKeys:
								@"http://www.activedeck.com/PowerPoint/", @"ad",
								nil];
    
    NSData *xmlData = [pInfo dataUsingEncoding:NSUTF8StringEncoding];
    
    GDataXMLDocument *pInfoDoc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
    GDataXMLElement *pSName = [[pInfoDoc nodesForXPath:@"//ad:Presentation" namespaces:namespaces error:nil] objectAtIndex:0];
    GDataXMLElement *accessNode = [[pInfoDoc nodesForXPath:@"//ad:Presentation/ad:UserAccess" namespaces:namespaces error:nil] objectAtIndex:0];
    
    pName = [[NSString alloc] initWithString:[[pSName attributeForName:@"Name"] stringValue]];
    
    NSString *userAccess = [[accessNode attributeForName:@"Value"]stringValue];
    
    
    if([userAccess isEqualToString:@"Anonymous"])
        pStatus = Anonymous;
    else
        pStatus = Locked;

    
    [pInfoDoc release];
    
    presentationID = [PresentationID intValue];

    return self;
}


-(void)dealloc {
    [pName release];
    [iconAddress release];
    [super dealloc];
}

@end
